package SupplierLevelAddition

import (
	"ToolLibray/jwtx"
	"context"
	"property_api/utils/result"
	"property_api/utils/xform"
	"property_service/property_service"

	"github.com/zeromicro/go-zero/core/logx"
	"property_api/internal/svc"
	"property_api/internal/types"
)

type EditLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewEditLogic(ctx context.Context, svcCtx *svc.ServiceContext) *EditLogic {
	return &EditLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

// Edit 修改供应商级别的逻辑
func (l *EditLogic) Edit(req *types.SupplierLevelAdditionEdit) (resp *types.Response, err error) {
	// 设置租户ID，从上下文中获取
	tenantID := jwtx.GetUserTenantId64(l.ctx)
	// 设置更新用户，从上下文中获取
	userIdStr := xform.Int64ToString(jwtx.GetUserId(l.ctx))

	// 调用服务层方法，修改供应商级别记录
	// 服务层方法 SupplierLevelAdditionEdit 会处理实际的数据库更新操作
	res, err := l.svcCtx.BasicService.SupplierLevelAdditionEdit(l.ctx, &property_service.SupplierLevelAddition{
		ID:                req.ID,                // 记录ID
		SupplierPerson:    req.SupplierPerson,    //供应商人
		SupplierPhone:     req.SupplierPhone,     //供应商电话
		Address:           req.Address,           //供应商地址
		TenantID:          tenantID,              // 租户ID
		SupplierLevelName: req.SupplierLevelName, // 供应商级别名称
		PronunciationCode: req.PronunciationCode, // 拼音简码
		Remark:            req.Remark,            // 备注
		UpdatedAtuser:     userIdStr,             // 更新用户
	})

	// 如果服务层返回错误，返回统一的错误响应
	if err != nil {
		return result.Err(err).End()
	}

	// 返回成功的响应
	return result.Suc(res).End()
}
